<?php

/**
 * @author     Artur Rozek
 * @version    1.0.0
 */
class sfWidgetFormDateJQueryUI extends sfWidgetForm
{
  /**
   * Configures the current widget.
   *
   * Available options:
   *
   * @param string   culture           Sets culture for the widget
   * @param boolean  change_month      If date chooser attached to widget has month select dropdown, defaults to false
   * @param boolean  change_year       If date chooser attached to widget has year select dropdown, defaults to false
   * @param integer  number_of_months  Number of months visible in date chooser, defaults to 1
   * @param boolean  show_button_panel If date chooser shows panel with 'today' and 'done' buttons, defaults to false
   * @param string   theme             css theme for jquery ui interface, defaults to '/sfJQueryUIPlugin/css/ui-lightness/jquery-ui.css'
   * 
   * @see sfWidgetForm
   */
  protected function configure($options = array(), $attributes = array())
  {

    if(sfContext::hasInstance())
     $this->addOption('culture', sfContext::getInstance()->getUser()->getCulture());
    else
     $this->addOption('culture', "en");
    $this->addOption('change_month',  false);
    $this->addOption('change_year',  false);
    $this->addOption('number_of_months', 1);
    $this->addOption('show_button_panel',  true);
    $this->addOption('theme', '/css/app/ui-lightness/jquery-ui.css');
    $this->addOption('button_img', '/images/icons/cal.png');
    $this->addOption('istimestamp', false);

    
    parent::configure($options, $attributes);
  }

  /**
   * @param  string $name        The element name
   * @param  string $value       The date displayed in this widget
   * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
   * @param  array  $errors      An array of errors for the field
   *
   * @return string An HTML tag string
   *
   * @see sfWidgetForm
   */
  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
    $attributes = $this->getAttributes();

    $input = new sfWidgetFormInput(array(), $attributes);
  	if($value!=null){
    	if(strpos($value,'-')===false){
    		if(strpos($value,'/')>0){
    			$html = $input->render($name, $value);
    		}
    	}else{
    		$timestamp = $this->getOption("istimestamp");
    		if($timestamp){
    			$dateTimeArray = explode(' ',$value);
    			$xdateStr = $dateTimeArray[0];
    			$timeStr = $dateTimeArray[1];
    		}else{
    			$xdateStr = $value;
    		}
    		$arrayDate = explode('-',$xdateStr);
    		if($timestamp){
	  			$timeArray = explode(':', $timeStr);
	  			$dateArray[3] = $timeArray[0];
	  			$dateArray[4] = $timeArray[1];
	  			$dateArray[5] = $timeArray[2];
	  			$html = $input->render($name, $arrayDate[2] . '/' . $arrayDate[1] . '/' . $arrayDate[0] . ' ' . $dateArray[3] . ':' . $dateArray[4]  . ':' . $dateArray[5]  );
	  		}else{
	  			$html = $input->render($name, $arrayDate[2].'/'.$arrayDate[1].'/'.$arrayDate[0]);
	  		}
    		
			
    	}	
    }else{
    	if($value===''){
    		$html = $input->render($name, '');
    	}else{
    		$html = $input->render($name, '');
    		//$html = $input->render($name, '__/__/____');
    	}
    }
    
    $id = $input->generateId($name);
    $culture = $this->getOption('culture');
    $cm = $this->getOption("change_month") ? "true" : "false";
    $cy = $this->getOption("change_year") ? "true" : "false";
    $nom = $this->getOption("number_of_months");
    $sbp = $this->getOption("show_button_panel") ? "true" : "false";
	$button = $this->getOption("button_img");
	$ts = $this->getOption("istimestamp") ? "true" : "false";
	
    
    $html .= <<<EOHTML
<script type="text/javascript">
	$(function() {
		setJqueryDatePicker("$id", "$culture", $ts, $cm, $cy, $sbp, $nom, "$button");
	});
</script>
EOHTML;

    return $html;
  }

  /*
   *
   * Gets the stylesheet paths associated with the widget.
   *
   * @return array An array of stylesheet paths
   */
  public function getStylesheets()
  {
    $theme = $this->getOption('theme');
    return array($theme => 'screen');
  }

  /**
   * Gets the JavaScript paths associated with the widget.
   *
   * @return array An array of JavaScript paths
   */
  public function getJavaScripts()
  {
    //check if jquery is loaded
    $js = array();
    /*if (sfConfig::has('sf_jquery_web_dir') && sfConfig::has('sf_jquery_core'))
      $js[] = sfConfig::get('sf_jquery_web_dir').'/js/'.sfConfig::get('sf_jquery_core');
    else
      $js[] = '/sfJQueryUIPlugin/js/jquery-1.3.1.min.js';
*/
    // $js[] = '/js/jQuery/jquery-ui.min.js';

    $culture = $this->getOption('culture');
    if ($culture!='en')
      $js[] = '/js/jQuery/i18n/ui.datepicker-'.$culture.".js";
    
    $timestamp = $this->getOption('istimestamp');
    if($timestamp){
    	$js[] = '/js/jQuery/jquery-ui-timepicker-addon.js';
    	$js[] = '/js/jQuery/jquery-ui-sliderAccess.js';
    };
    return $js;
  }

}
